package com.lzy.imagepickerdemo.wxdemo;


import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.view.ViewHolder;
import com.lzy.imagepickerdemo.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：ikkong （ikkong@163.com），修改 jeasonlzy（廖子尧）
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：微信图片选择的Adapter, 感谢 ikkong 的提交
 * ================================================
 */
public class ImagePickerAdapter extends RecycleItemProvider {
    private int maxImgCount;
    private Context mContext;
    private List<ImageItem> mData;
    private LayoutScatter mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean isAdded;   //是否额外添加了最后一个图片

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        SelectedPicViewHolder viewHolder = onCreateViewHolder(componentContainer, 0);
        onBindViewHolder(viewHolder, i);
        return viewHolder.getRootView();
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(Component view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<ImageItem> data) {
        mData = new ArrayList<>(data);
        if (getCount() < maxImgCount) {
            mData.add(new ImageItem());
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataChanged();
    }

    public List<ImageItem> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        if (isAdded) return new ArrayList<>(mData.subList(0, mData.size() - 1));
        else return mData;
    }

    public ImagePickerAdapter(Context mContext, List<ImageItem> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.mInflater = LayoutScatter.getInstance(mContext);
        setImages(data);
    }

    public SelectedPicViewHolder onCreateViewHolder(ComponentContainer parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.parse(ResourceTable.Layout_list_item_image, parent, false));
    }

    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }


    public class SelectedPicViewHolder extends ViewHolder implements Component.ClickedListener {

        private Image iv_img;
        private int clickPosition;

        public SelectedPicViewHolder(Component itemView) {
            iv_img = (Image) itemView.findComponentById(ResourceTable.Id_iv_img);
        }

        public void bind(int position) {
            //设置条目的点击事件
            iv_img.setClickedListener(this);
            //根据条目位置设置图片
            ImageItem item = mData.get(position);
            iv_img.setPixelMap(ResourceTable.Media_image_add_nor);
            if (isAdded && position == getCount() - 1) {
                iv_img.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
                    @Override
                    public void onComponentStateChanged(Component component, int status) {
                        switch (status) {
                            case ComponentState.COMPONENT_STATE_FOCUSED:
                            case ComponentState.COMPONENT_STATE_PRESSED:
                            case ComponentState.COMPONENT_STATE_SELECTED:
                                iv_img.setPixelMap(ResourceTable.Media_image_add_sel);
                                break;
                            default:
                                iv_img.setPixelMap(ResourceTable.Media_image_add_nor);
                                break;
                        }
                    }
                });
                clickPosition = WxDemoAbility.IMAGE_ITEM_ADD;
            } else {
                ImagePicker.getInstance().getImageLoader().displayImage((Ability) mContext, item.uriSchema, iv_img, 0, 0);
                clickPosition = position;
            }
        }

        @Override
        public void onClick(Component v) {
            if (listener != null) listener.onItemClick(v, clickPosition);
        }

        @Override
        public Component getRootView() {
            return iv_img;
        }
    }
}